package com.zzyl.serve.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.PageDomain;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.core.page.TableSupport;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 25920
 */
@Api(tags = "小程序的护理项目相关")
@Slf4j
@RestController
@RequestMapping("/member/orders/project")
public class MemberOrderController extends BaseController {

    @Autowired
    private INursingProjectService nursingProjectService;
    @ApiOperation("1.1 分页查询护理项目列表")
    @GetMapping("/page")
    public TableDataInfo getProjectByPage(NursingProject nursingProject) {
        //拿到分页页码和大小，然后进行判断，是否为超过总数量的访问
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Integer checkTotal = pageSize * pageNum;
        //开始分页
        startPage();
        List<NursingProject> list = nursingProjectService.selectNursingProjectList(nursingProject);
        TableDataInfo dataTable = getDataTable(list);
        //如果查询的数量大于 分页页码-1 * 分页大小 再返回正常数据，否则返回空集合
        if (!StringUtils.isEmpty(nursingProject.getName()) || dataTable.getTotal() > (checkTotal - pageSize)) {
            return dataTable;
        } else {
            return getDataTable(new ArrayList<>())   ;
        }
    }

    @ApiOperation("1.2 根据编号查询护理项目信息")
    @GetMapping("/{id}")
    public AjaxResult getProjectById(@PathVariable Long id) {
        NursingProject project = nursingProjectService.getById(id);
        return AjaxResult.success(project);
    }

}
